package org.csu.webjpetstoreback.controller;

import jakarta.servlet.http.HttpSession;
import org.csu.webjpetstoreback.config.CommonResponse;
import org.csu.webjpetstoreback.model.Item;
import org.csu.webjpetstoreback.model.Orders;
import org.csu.webjpetstoreback.model.Product;
import org.csu.webjpetstoreback.service.ItemService;
import org.csu.webjpetstoreback.service.ProductService;
import org.csu.webjpetstoreback.service.serviceImpl.ProductServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@SessionAttributes(names = "httpSession")
@Validated
public class ProductController {

  @Autowired
  public ProductService productService;

  @Autowired
  public ItemService itemService;
  @GetMapping("/Products/{categoryId}")
    public CommonResponse<List<Product>> GetProductListByCategory(@PathVariable("categoryId")String categoryId){
        return productService.GetProductsListByCategory(categoryId);
    }



  @GetMapping("/Products/{Keywords}")
    public CommonResponse<List<Product>> GetProductListByKeywords(@PathVariable("Keywords")String Keywords){
    return productService.GetProductListByKeywords(Keywords);
  }


  @GetMapping("/Product/{ProductId}")
    public  CommonResponse<Product>GetProductByProductId(@PathVariable("ProductId")String ProductId){
      return productService.GetProductByProductId(ProductId);
  }

  //这个方法是获取main函数当中获取的product对象
  @GetMapping("/Product/setProductSession/{ProductId}")
  public  CommonResponse<Product>setProductSession(@PathVariable("ProductId")String ProductId, HttpSession session){
    session.setAttribute("productSession",ProductId);
    String productId2= (String) session.getAttribute("productSession");
    return CommonResponse.BackForSuccess("保存ProductSession成功");
  }

  @GetMapping("/Product/getItemByNothing")
  public  CommonResponse < List<Item>> getItemByNothing(HttpSession session){
    String productId= (String) session.getAttribute("productSession");
    System.out.println(productId);
    System.out.println("wwww");
    return itemService.GetItemListByProductId(productId);
  }

  @PostMapping("/Products")
    public CommonResponse<Product> PostProduct(@RequestBody Product product){
    return productService.PostProducts(product);
  }


  @PutMapping("/Products")
    public CommonResponse<Product> PutProduct(@RequestBody Product product){
      return productService.PutProducts(product);
  }



  @DeleteMapping("/Products/{id}")
    public CommonResponse<Product> DeleteProduct(@PathVariable("id") int productId){
      return productService.DeleteProducts(productId);
  }

}
